﻿/*
对话框组件
开发人:糊涂书生 www.723.la
版本:0.1
*/
(function ($) {
    $.dboxDialog = function (d) {
        var def = {
            id: '',
            title: '',
            content: '',
            ok: null,
            okText: '确定',
            cancel: true,
            cancelText: '取消',
            width: null,
            height: null,
            padding: null,
            zIndex: null,
            button: null,
            follow: null,
            opacity: null,
            time:null
        };
        var result = {
            obj: null,
            show: function () {
                $(this.obj).show();
            },
            close: function () {
                $(this.obj).remove();
                $("#dbox_opacity_" + def.id).remove();
            }
        };
        //启动值
        var init = function (d) {
            var t = new Date();
            def.id = t.getHours().toString() + t.getMinutes().toString() + t.getSeconds().toString() + t.getMilliseconds().toString();
            setValue(d);
            var htm = getHtm().join('');
            $("body").append(htm);
            result.obj = $("#dbox_dialog_" + def.id);
            //居中位置设置
            $(result.obj).css("top", ($(window).height() / 2 + $(document).scrollTop()) - $(result.obj).height() / 2);
            $(result.obj).css("left", ($(window).width() / 2) - $(result.obj).width() / 2);
            //跟随元素
            Follow();
            //遮罩层
            Opacity();
            //定时关闭
            if (def.time != null) {
                setInterval(function () {
                    result.close();
                }, def.time);
            }
           
            //事件绑定
            eventBind();

            //鼠标拖拽事件
            MouseEvent();
        };
        //设置初始值
        var setValue = function (d) {
            if (d.id != null) {
                def.id = d.id;
            }
            if (d.title != null) {
                def.title = d.title;
            }
            if (d.content != null) {
                def.content = d.content;
            }
            if (d.ok != null) {
                def.ok = d.ok;
            }
            if (d.okText != null) {
                def.okText = d.okText;
            }
            if (d.cancel != null) {
                def.cancel = d.cancel;
            }
            if (d.cancelText != null) {
                def.cancelText = d.cancelText;
            }
            if (d.width != null) {
                def.width = d.width;
            }
            if (d.height != null) {
                def.height = d.height;
            }
            if (d.padding != null) {
                def.padding = d.padding;
            }
            if (d.zIndex != null) {
                def.zIndex = d.zIndex;
            }
            if (d.button != null) {
                def.button = d.button;
            }
            if (d.follow != null) {
                def.follow = d.follow;
            }
            if (d.opacity != null) {
                def.opacity = d.opacity;
            }
            if (d.time != null) {
                def.time = d.time;
            }
        };
        //按钮事件绑定
        var eventBind = function () {
            var bid = "dbox_dialog_button_" + def.id;
            if (def.cancel != null) {
                $("#" + bid).append('<input name="" type="button" id="dbox_dialog_button_cancel_' + def.id + '"  class="dbox_cancel" value="' + def.cancelText + '" />');
                $("#dbox_dialog_button_cancel_" + def.id).on("click",
                function () {
                    result.close();
                });
            }
            if (def.ok != null) {
                $("#" + bid).append('<input name="" type="button" id="dbox_dialog_button_ok_' + def.id + '" class="dbox_sure" value="' + def.okText + '" />');
                $("#dbox_dialog_button_ok_" + def.id).on("click", def.ok);
            }
            $("#dbox_dialog_close_" + def.id).on("click",
            function () {
                result.close();
            });
            if (def.button != null && def.button.length > 0) {
                var len = def.button.length;
                var b;
                for (var i = 0; i < len; i++) {
                    b = def.button[i];
                    var id = 'dbox_dialog_button_custom' + i + '_' + def.id;
                    $("#" + bid).append('<input name="" type="button" id="' + id + '" class="dbox_sure" value="' + b.text + '" />');
                    $("#" + id).on("click", b.callback);
                }
            }
            
        };
        //对话框HTML文本
        var getHtm = function () {
            var s = new Array();
            s.push(def.width != null ? 'width:' + def.width + 'px;' : '');
            s.push(def.height != null ? 'height:' + def.height + 'px;' : '');
            var htm = new Array();
            htm.push('<div class="dbox_dialog" style="' + (def.zIndex != null ? 'z-index:' + def.zIndex + ';' : '') + '" id="dbox_dialog_');
            htm.push(def.id);
            htm.push('" >');
            htm.push('<div class="dbox_title">');
            htm.push('<div class="dbox_title_tip" id="dbox_dialog_title_' + def.id + '">' + def.title + '</div>');
            htm.push('<div class="dbox_title_close" id="dbox_dialog_close_' + def.id + '"></div>');
            htm.push('</div>');
            htm.push('<div class="dbox_content"  style="' + s.join('') + '" id="dbox_dialog_content_' + def.id + '">');
            htm.push(def.content);
            htm.push('</div>');
            htm.push('<div class="dbox_button" id="dbox_dialog_button_' + def.id + '">');
            htm.push('</div>');
            htm.push('</div>');
            return htm;
        };
        //处理遮罩层
        var Opacity = function () {
            if (def.opacity != null) {
                var htm = '<div class="dbox_opacity" id="dbox_opacity_' + def.id + '"></div>';
                $("body").append(htm);
                var opa = $("#dbox_opacity_" + def.id);
                $(opa).css("opacity", def.opacity);
                $(opa).css("height", $(document).height());
            }
        };
        //对话框跟随某个元素
        var Follow = function () {
            if (def.follow != null) {
                var l, t, w = $(result.obj).width(), h = $(result.obj).height();
                l = $(def.follow).offset().left;
                t = $(def.follow).offset().top + $(def.follow).height() + 10;
                $(result.obj).css("left", l);
                $(result.obj).css("top", t);
            }
        };
        //鼠标拖拽
        var MouseEvent = function () {
            var mt = $("#dbox_dialog_title_" + def.id);
            $(mt).css("width", ($(result.obj).width() - 30));
            $(mt).css("height", 40);
            $(mt).on("mousedown", function (e) {
                //对话框所在的位置
                var offset = $(this).offset();
                //获取x y坐标
                var x = e.pageX - offset.left;
                var y = e.pageY - offset.top;
                //绑定拖拽事件
                $(document).on("mousemove", function (ev) {
                    $(mt).stop();
                    var _x = ev.pageX - x;
                    var _y = ev.pageY - y;
                    $(result.obj).css("left", _x);
                    $(result.obj).css("top", _y);
                });
                $(document).on("mouseup", function () {
                    $(this).unbind("mousemove");
                });
               
            });
        };
        init(d);
        
        return result;
    };
})(jQuery);